package com.mingzhi.lotterysystem.service.activitystatus.operator;

import com.mingzhi.lotterysystem.dao.dataobject.ActivityUserDO;
import com.mingzhi.lotterysystem.dao.mapper.ActivityUserMapper;
import com.mingzhi.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.mingzhi.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * UserOperator:人员操作符
 */
@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //判断人员状态
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        if(null == activityId || CollectionUtils.isEmpty(userIds) || null == targetUserStatus){
            return false;
        }
        List<ActivityUserDO> activityUserDOList = activityUserMapper.batchSelectByAUIds(activityId, userIds);
        if (null == activityUserDOList){
            return false;
        }
        for (ActivityUserDO activityUserDO : activityUserDOList){
            if (activityUserDO.getStatus().equalsIgnoreCase(targetUserStatus.name())){
                return false;
            }
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetUserStatus);
            return true;
        }catch (Exception e){
            return false;
        }
    }


}
